****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************

clear all
set more off
set matsize 800

global lotcons "female black white hispanic lunch?? lunch_miss app2 app3 anyt109 anyt110 anyt111 anyt112 anyt1lunch?? esly??"  

global nccons "math_lag read_lag science_lag math_gr_lag read_gr_lag mardgr_int_lag science_gr_lag allgr_int_lag i.lowread_nonmag#i.year lep_Y_lag ma_aig_lag rd_aig_lag aig_int_lag"


** kindergarten sample
	
use all-lottery-movers-analysis.dta , clear 

keep if (year>2008 & ksample & stayer==1)

gen mathstand_diff = mathstand_at - mathstand_as
gen readstand_diff = readstand_at - readstand_as

gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_at - avgtestscores_as

gen notmove1 = (1 - move1)

gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1

gen move1_lost = (1 - wonfirst)*move1

gen notmove1_lost = notmove1*(1 - wonfirst)

gen attassschl = (curr_lead1==assschl)

gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier

gen move1_win_noncomp = win_noncomplier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1

xtset lottery

tempfile ksample 
	save `ksample' 

global outcomecats "move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp"

reg avgscore_diff $outcomecats $lotcons i.lottery , cluster(lottery)

estadd scalar N_ind = e(N)

est store A

** sixth grade
use all-lottery-movers-analysis.dta , clear 

keep if (year>2008 & sixsample & stayer==1)

gen mathstand_diff = mathstand_at - mathstand_as
gen readstand_diff = readstand_at - readstand_as

gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_at - avgtestscores_as

gen notmove1 = (1 - move1)

gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1

gen move1_lost = (1 - wonfirst)*move1

gen notmove1_lost = notmove1*(1 - wonfirst)

gen attassschl = (curr_lead1==assschl)

gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier

gen move1_win_noncomp = win_noncomplier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1

xtset lottery

tempfile sixsample 
	save `sixsample' 

reg avgscore_diff $outcomecats $lotcons $nccons i.lottery , cluster(lottery)

estadd scalar N_ind = e(N)

est store B

esttab A B using table4.tex , replace ///
mtitles("Avg. MA/RD" "Avg. MA/RD") ///
refcat(move1_lost "\emph{Lottery Loser}" move1_win_noncomp "\emph{Lottery Winner}" , nolabel ) ///
label booktabs nonum gaps f noobs collabels(none)  ///
cells("b(fmt(3)star)" "se(fmt(3)par)") star(* 0.10 ** 0.05 *** 0.01) keep(move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp) ///
coef(move1_lost "Change HS" notmove1_lost "Same HS" move1_win_noncomp "Non-Complier X Change HS"  notmove1_win_noncomp "Non-Complier X Same HS") 
 
  
esttab A B using table4.tex , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(N_ind , fmt(%10.0fc) layout("\multicolumn{1}{c}{@}") labels(`"Observations"'))













